function mergeSort(arr) {
  if(arr.length <= 1) return arr
  const midIndex = arr.length / 2 
  const leftArr = arr.slice(0, midIndex)
  const rightArr = arr.slice(midIndex)
  return merge(mergeSort(leftArr), mergeSort(rightArr))
}
function merge(leftArr, rightArr) {
  const result = []
    while(leftArr.length && rightArr.length) {
    	leftArr[0] <= rightArr[0] ? result.push(leftArr.shift()) : result.push(rightArr.shift())
    }
    while(leftArr.length) result.push(leftArr.shift())
    while(rightArr.length) result.push(rightArr.shift())
    return result
}

const arr = [3, 5, 1, 2, 9, 6, 10]
console.log(mergeSort(arr))